﻿using System;
using System.Collections.Generic;
using System.Linq;
using Hourglass.Qrobot.Http;

namespace Hourglass.Qrobot.Models
{
    public sealed class FriendsManager
    {
        /// <summary>
        /// 所有好友
        /// </summary>
        public Dictionary<long, Friend> Friends { get; set; }

        /// <summary>
        /// 好友组别
        /// </summary>
        public Dictionary<long, FriendCatrgory> Catrgories { get; set; }

        /// <summary>
        /// 获取最近联系好友
        /// </summary>
        public List<Friend> RecentFriends => Friends.Values.Where(f => f.IsRecent).ToList();

        public FriendsManager()
        {
            this.Friends = new Dictionary<long, Friend>();
            this.Catrgories = new Dictionary<long, FriendCatrgory>
            {
                { 0, new FriendCatrgory(0, 0, "我的好友") }
            };
        }

        public Friend FindFriendById(long id)
        {
            if (Friends.ContainsKey(id))
            {
                return Friends[id];
            }
            return null;
        }

        public Friend FindFriendByQq(long qq)
        {
            return Friends.Values.FirstOrDefault(f => f.Qq.Equals(qq));
        }
    }

    /// <summary>
    /// 好友信息
    /// </summary>
    public class Friend : IPerson
    {
        private string _nickName;
        private string _markName;

        /// <summary>
        /// 用户ID
        /// </summary>
        /// <remarks>Uin</remarks>
        public long Id { get; set; }

        /// <summary>
        /// QQ号码
        /// </summary>
        public long Qq { get; set; }

        /// <summary>
        /// 好友组别
        /// </summary>
        public FriendCatrgory Catrgory { get; set; }

        #region 昵称、备注、个人说明、VIP等级

        /// <summary>
        /// 昵称
        /// </summary>
        public string NickName
        {
            get
            {
                if (string.IsNullOrEmpty(_nickName))
                {
                    return Qq.ToString();
                }
                return _nickName;
            }
            set { _nickName = value; }
        }

        /// <summary>
        /// 备注名称
        /// </summary>
        public string MarkName
        {
            get
            {
                if (string.IsNullOrEmpty(_markName))
                {
                    return NickName;
                }
                return _markName;
            }
            set { _markName = value; }
        }

        /// <summary>
        /// 个人说明
        /// </summary>
        public string Personal { get; set; }

        /// <summary>
        /// VIP等级
        /// </summary>
        public int VipLevel { get; set; }

        /// <summary>
        /// 是否是最近联系人
        /// </summary>
        public bool IsRecent { get; set; }

        /// <summary>
        /// 状态[在线/离线 等]
        /// </summary>
        public SmartEnums.OnlineStatus Status { get; set; }

        /// <summary>
        /// 登录类型[手机/PC 等]
        /// </summary>
        public SmartEnums.OnlineClientType ClientType { get; set; }

        #endregion 账号、昵称、个人说明

        #region 个人栏：性别、生日、生肖、星座、血型

        /// <summary>
        /// 性别
        /// </summary>
        public SmartEnums.Gender Gender { get; set; }

        /// <summary>
        /// 生日
        /// </summary>
        public DateTime? Birthday { get; set; }

        /// <summary>
        /// 生肖
        /// </summary>
        public SmartEnums.Zodiac Zodiac { get; set; }

        /// <summary>
        /// 星座
        /// </summary>
        public SmartEnums.Constel Constel { get; set; }

        /// <summary>
        /// 血型
        /// </summary>
        public SmartEnums.Blood Blood { get; set; }

        #endregion 个人栏：性别、生日、生肖、星座、血型

        #region 联系方式：电话、手机、主页、邮箱、所在地、邮编

        /// <summary>
        /// 所在地
        /// </summary>
        public Location Location { get; set; }

        /// <summary>
        /// 电话
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// 手机
        /// </summary>
        public string Mobile { get; set; }

        /// <summary>
        /// 主页
        /// </summary>
        public string Homepage { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email { get; set; }

        #endregion 联系方式：电话、手机、主页、邮箱、地址、邮编

        #region 经历信息：学历、大学名称

        /// <summary>
        /// 职业/学历
        /// </summary>
        public string Occupation { get; set; }

        /// <summary>
        /// 大学名称
        /// </summary>
        public string College { get; set; }

        #endregion 经历信息

        #region 未知元素

        public int Face { get; set; }

        public int Allow { get; set; }

        public long Flag { get; set; }

        /// <summary>
        /// 取自好友列表marknames/type
        /// </summary>
        public int MarkNameType { get; set; }

        #endregion 未知元素

        public QqMessage.PersonInfo GetPersonInfo()
        {
            return new QqMessage.PersonInfo
            {
                //Id = Id,
                Qq = Qq,
                NickName = NickName,
                MarkName = MarkName
            };
        }
    }

    public sealed class FriendCatrgory
    {
        /// <summary>
        /// ID
        /// </summary>
        /// <remarks>Index</remarks>
        public long Id { get; set; }

        /// <summary>
        /// 实际排序
        /// </summary>
        /// <remarks>Sort</remarks>
        public int Order { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 好友成员
        /// </summary>
        public Dictionary<long, Friend> Friends { get; set; }

        public FriendCatrgory() { }

        public FriendCatrgory(long id, int order, string name)
        {
            this.Id = id;
            this.Order = order;
            this.Name = name;
            Friends = new Dictionary<long, Friend>();
        }
    }
}
